/**
 * 扩展函数
 * Created by YFSZ on 2019-08-21 09:44:57.
 * 自动生成
 */
import Vue from 'vue'
import { Message } from 'element-ui'
import _store from "@/store";

Vue.prototype.getDictListCode = (DictTypeCode) => {
    let statusList = (DictTypeCode) => {
        return _store.getters.dictionary.filter(item => item.DictTypeCode === DictTypeCode)
    }
    return statusList(DictTypeCode)
}
Vue.prototype.getCode = (cellValue, DictTypeCode) => {
    let statusList = (DictTypeCode) => {
        return _store.getters.dictionary.filter(item => item.DictTypeCode === DictTypeCode)
    }
    let filter = statusList(DictTypeCode).filter(it => it.DictId === (cellValue || '').toString())
    if (filter.length > 0) {
        return filter[0].DictName
    }
    return ''
}
/* *
 * 加密
 */
Vue.prototype.StringEncode = function (str) {  //加密字符串
    let c = String.fromCharCode(str.charCodeAt(0) + str.length);

    for (let i = 1; i < str.length; i++) {
        c += String.fromCharCode(str.charCodeAt(i) + str.charCodeAt(i - 1));
    }

    return encodeURIComponent(c);
}

/* *
 * 解密
 */
Vue.prototype.StringDecode = function (str) {  //加密字符串
    str = decodeURIComponent(str);
    let c = String.fromCharCode(str.charCodeAt(0) - str.length);

    for (let i = 1; i < str.length; i++) {
        c += String.fromCharCode(str.charCodeAt(i) - c.charCodeAt(i - 1));
    }
    return c;
}
/**
 * 获取对象是否为null或undefined或空字符串
 *
 * @param {Object} obj 检查对象
 */
Vue.prototype.UtilJs = {
    isNull: function (obj) {
        return obj === undefined || obj === null
    },
    isEmptyObject: function (obj) {
        if (typeof obj === 'object' && !(obj instanceof Array)) {
            for (var prop in obj) {
                return false
            }
            return true
        }
        return false
    },
    isEmpty: function (obj) {
        return obj === undefined || obj == null || this.isEmptyObject(obj) || obj === ''
    },
    isFunction: function (obj) {
        return typeof obj === 'function'
    },
    compareDate: function (str1, str2) {
        if (new Date(str1).valueOf() > new Date(str2).valueOf()) {
            return 1
        } else if (new Date(str1).valueOf() < new Date(str2).valueOf()) {
            return -1
        } else if (new Date(str1).valueOf() === new Date(str2).valueOf()) {
            return 0
        }
    },
    // 深度clone
    cloneDeep: function (obj) {
        let newObj = Array.isArray(obj) ? [] : {}
        if (obj && typeof obj === 'object') {
            for (let key in obj) {
                if (obj.hasOwnProperty(key)) {
                    newObj[key] = (obj && typeof obj[key] === 'object') ? this.cloneDeep(obj[key]) : obj[key]
                }
            }
        }
        return newObj
    }
}

const duration = 3000
const showClose = false

Vue.prototype.success = function (msg) {
    Message({
        showClose: showClose,
        message: msg,
        type: 'success',
        duration: duration
    })
}

Vue.prototype.error = function (msg) {
    Message({
        showClose: showClose,
        message: msg,
        type: 'error',
        duration: duration
    })
}

Vue.prototype.info = function (msg) {
    Message({
        showClose: showClose,
        message: msg,
        type: 'info',
        duration: duration
    })
}

Vue.prototype.warning = function (msg) {
    Message({
        showClose: showClose,
        message: msg,
        type: 'warning',
        duration: duration
    })
}
